#!/usr/bin/env python
#encoding:utf-8
#Create：     2013-7-22
#Author：     longlimin

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "hive_gen"))

from hive import ThriftHive
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
 
import ins
import traceback
import config

class HiveExcuter:
    
    HIVE_FIELD = config.HIVE_SEP_FIELD 
    HIVE_ITEMS = config.HIVE_SEP_ITEMS
    HIVE_MAP_KEYS = config.HIVE_SEP_KEYS
    
    LOG_FILE_NAME = "0000_00"
    
    def __init__(self, host, port, fuse_root, db='infoc'):
        self.host, self.port, self.db = host, port, db
        self.fuse_root = fuse_root
        self._init()
    
    def _init(self):
        try:
            self.transport = TSocket.TSocket(self.host, self.port)
            self.transport = TTransport.TBufferedTransport(self.transport)
            protocol       = TBinaryProtocol.TBinaryProtocol(self.transport)
            self.hiveClient = ThriftHive.Client(protocol)
            self.transport.open()
            self.hiveClient.execute("use %s" % self.db)
        except Thrift.TException, tx:
            ins.errlog("HIVE CONN ERROR  %s" % tx.message)
        except:
            ins.errlog(traceback.format_exc())
            
    def __del__(self):
        self.transport.close()
 
    def get_all_tables(self):
        return self.hiveClient.get_all_tables(self.db)
    
    def is_exists(self, tabl_name):
        try:
            self.hiveClient.get_table(self.db, tabl_name)
            return True
        except:
            return False
    
    def create_table(self, tabl_name, fields=[]):
        
        if  self.is_exists(tabl_name):
            return 
        cmd = "CREATE TABLE %s(" % tabl_name
        for row in fields:
            cmd += "%s %s," % (row["code"], ins.HIIVE_TYPE.get(row["datatype"], "string"))
        cmd += "others MAP<STRING, STRING>) PARTITIONED BY(dt STRING) ROW FORMAT DELIMITED \
			    FIELDS TERMINATED BY '%s' COLLECTION ITEMS TERMINATED BY '%s' MAP KEYS TERMINATED BY '%s'\
                STORED AS TEXTFILE" % (self.HIVE_FIELD, self.HIVE_ITEMS, self.HIVE_MAP_KEYS)
        print cmd
        return self.hiveClient.execute(cmd)
        
    def load_data(self, root, tabl_name, date):
        logpath = os.path.join(root, tabl_name , "%s.txt" % date)
        self.hiveClient.execute("ALTER TABLE %s ADD IF NOT EXISTS PARTITION (dt='%s')" % (tabl_name, date))
        fuse_log = os.path.join(self.fuse_root, tabl_name, "dt=%s"%date, self.LOG_FILE_NAME)
        if not os.path.exists(fuse_log):
            os.system("touch %s" % fuse_log)
        cmd = "cat %s >> %s" % (logpath, fuse_log)
        os.system(cmd)
        cmd = "cat %s >> %s.done" % (logpath, logpath)
        os.system(cmd)
        if len(logpath) > 20: #如果logpath 
            cmd = "rm -rf %s" % logpath
            os.system(cmd)
 
if __name__ == "__main__":
    import utils
    hiveExe = HiveExcuter(*config.HIVE)
    code = "duba_shouji_maindata"
    print hiveExe.create_table(code, utils.get_fields(code)) 
    print hiveExe.load_data("/data/logs/hiveimport", "duba_shouji_maindata", "2013-07-29")
    #print hiveExe.is_exists("duba_iestart_dest1")
    #print hiveExe.load_data("/tmp/shouji_maindata.txt", "test_shouji_main")
